package com.chidopi.cdf.dao.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;

import com.chidopi.cdf.dao.ICommonDao;


public class CommonDaoImpl implements ICommonDao {
	private static Logger logger = LoggerFactory.getLogger(CommonDaoImpl.class);

	protected SimpleJdbcTemplate simpleJdbcTemplate;
	@Autowired
	public void init(DataSource datasource) {
		this.simpleJdbcTemplate = new SimpleJdbcTemplate(datasource);
		logger.debug("init: after new JdbcTemplate");
	}

	public List<Map<String, Object>> queryForList(String sql, Object... args) {
		logger.debug("SQL:[" + sql + "]/params:" + Arrays.asList(args).toString());
		return this.simpleJdbcTemplate.queryForList(sql, args);
	}

	public int executeSQL(String sql, Object... args) {
		return this.simpleJdbcTemplate.update(sql,args);
	}

	public void executeSQL(String[] sql) {
		if (sql == null) throw new NullPointerException("sql is null!!");
		for (String str : sql) {
			this.simpleJdbcTemplate.update(str);
			logger.debug("success: " + str);
		}
	}
}
